<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/14
 * Time: 9:43
 */

namespace app\admin\controller;

use app\common\controller\Backend;
use fast\Http;
use think\addons\AddonException;
use think\addons\Service;
use think\Cache;
use think\Config;
use think\Exception;
use think\Db;

class Goods extends Backend
{
    public function _initialize()
    {
        parent::_initialize();
    }
	
	/*
	 * 商品管理首页
	 */
    public function index(){
    	$where = $this->request->post('where');
    	if($where == "" || $where == null){
    	    //库存大于0状态不为下架，不存在回收站
    		$where = "inventory > 0 AND status != 2 AND is_recycle =1";
    	}
		$list = Db::name('goods')->field("id,sort,name,selling_price,inventory,sale_quantity,ac_sale_quantity,status,attribute")->where($where)->paginate(40);
		$this->assign('list', $list);
        return $this->view->fetch("list");
    }
    
    /*
     * 添加商品页面
     */
    public function add(){
    	return $this->view->fetch("edit");
    }

    /*
     * 编辑商品页面
     */
    public function edit($id = null){
        return $this->view->fetch("edit");
    }
    
	/*
	 * 已售罄首页
	 */
	public function sellOut(){
		$where = $this->request->post('where');
    	if($where == "" || $where == null){
    	    //库存小于等于0状态不为下架，不存在回收站
    		$where = "inventory = 0 AND inventory < 0 AND status != 2 AND is_recycle =1";
    	}
		$list = Db::name('goods')->field("id,sort,name,selling_price,inventory,sale_quantity,ac_sale_quantity,status,attribute")->where($where)->paginate(40);
		$this->assign('list', $list);
		return $this->view->fetch("list");
	}
	
	/*
	 * 仓库中首页
	 */
	public function warehouse(){
		$where = $this->request->request('where');
    	if($where == "" || $where == null){
    		//状态为下架
    		$where = "status = 2";
    	}
    	$list = Db::name('goods')->field("id,sort,name,selling_price,inventory,sale_quantity,ac_sale_quantity,status,attribute")->where($where)->paginate(40);
		$this->assign('list', $list);
		return $this->view->fetch("list");
	}
	
	/*
	 * 回收站首页
	 */
	public function recycle(){
		$where = $this->request->request('where');
    	if($where == "" || $where == null){
    		//已被添加到回收站中
    		$where = "is_recycle = 2";
    	}
    	$list = Db::name('goods')->field("id,sort,name,selling_price    ,inventory,sale_quantity,ac_sale_quantity,status,attribute")->where($where)->paginate(40);
		$this->assign('list', $list);
		return $this->view->fetch("list");
	}
	
	/*
	 * 商品分类首页
	 */
	public function category(){
		$list = Db::name('goods_category')->field("id,name,describe,advertisement,url,show")->select();
		$this->assign('list', $list);
		return $this->view->fetch();
	}
	
	/*
	 * 标签管理首页
	 */
	public function label(){
		$list = Db::name('goods_label')->field("id,name,status,pid")->select();
		$this->assign('list', $list);
		echo "<pre>";
		print_r($list);
		return $this->view->fetch();
	}
	
	public function toarr($array = array()){
		$count = count($array);
		$newarr = array();
		for ($m = 0;$m < $count; $m++){
            if($array[$m]["id"] == 0){
                $newarr["$m"]["id"] = $array[$m]["id"];
            }
        }
        return $newarr;
	}
	
	/*
	 * 添加到回收站
	 */
	public function addRecycle(){
		$ids = $this->request->post('ids');
		$result = Db::name("goods")->where("id IN (".$ids.")")->update("is_recycle = 2");
		if($result){
            return json(array("status"=>1,"info"=>"添加成功！"));
        }else{
            return json(array("status"=>0,"info"=>"添加失败！"));
        }
	}
	
	/*
	 * 真实删除
	 */
	public function delete(){
        $ids = $this->request->post('ids');
        $result = Db::name("goods")->where("id IN (".$ids.")")->delete();
        if($result > 0){
            return json(array("status"=>1,"info"=>"删除".$result."条"));
        }else{
            return json(array("status"=>0,"info"=>"删除失败！"));
        }
	}

    /*
     * 编辑和添加的方法
     * 根据type判断添加或编辑的是哪张表
     */
    public function editAdd(){
        $type = $this->request->post('type');
        if($type == "" || $type == null){
            return json(array("status"=>0,"info"=>"发生错误，请输入正确的类型"));
        }
        
        $table_name = "";//需要操作的表
        $name = "";//名称
        
        switch ($type){
            case 1://商品
                if($this->request->post('goods_pic') == "" || $this->request->post('goods_pic') == null){
                    return json(array("status"=>0,"info"=>"请上传商品图片"));
                }
                if($this->request->post('selling_price') == "" || $this->request->post('selling_price') == null){
                    return json(array("status"=>0,"info"=>"请填写商品售价"));
                }
                $name = "商品名称";
                $table_name = "goods";
                break;
            case 2://商品分类
                $name = "名称名称";
                $table_name = "goods_category";
                break;
            case 3://运费模板
                $name = "模板名称";
                $table_name = "goods_freight";
                break;
            case 4://商品标签
                $name = "标签名称";
                $table_name = "goods_label";
                break;
            default:
                return json(array("status"=>0,"info"=>"请填写正确type"));
        }
        
        if($this->request->post('name') == "" || $this->request->post('name') == null){
            return json(array("status"=>0,"info"=>"请填写".$name));
        }
        
        //如果有id则是编辑，否则是添加
        if($this->request->post('id') == "" || $this->request->post('id') == null){
            $data = request()->post();
            $data["id"] = uniqid();
            $result = Db::name($table_name)->insert($data);
            $id = $data["id"];
        }else{
            $data = request()->post();
            $id = $this->request->post('id');
            $result = Db::name($table_name)>where('id', $id)->update($data);
        }
        return json(array("status"=>$result,"info"=>"添加成功","id"=>$id));

    }

}